from rest_framework.decorators import action

from meiduo_admin.serializers.orders import OrdersSerializer, OrderSerializer
from orders.models import OrderInfo
from meiduo_admin.serializers.options import OptionsSerializer, SimpleSerializer
from meiduo_admin.utils import PageNum1
from rest_framework.response import Response
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.viewsets import ReadOnlyModelViewSet


# class OrdersView(ListAPIView):
#     queryset = OrderInfo.objects.all()
#     serializer_class = OrdersSerializer
#     pagination_class = PageNum1
#
#
# class OrderView(RetrieveAPIView):
#     queryset = OrderInfo.objects.all()
#     serializer_class =OrderSerializer

class OrderViewSet(ReadOnlyModelViewSet):
    queryset = OrderInfo.objects.all()
    serializer_class = OrderSerializer
    pagination_class = PageNum1

    @action(methods=['put'], detail=True)
    def status(self, request, pk):
        data = OrderInfo.objects.get(order_id=pk)
        data.status = request.data.get('status')
        data.save()
        ser = OrderSerializer(data)
        return Response(ser.data)
        # order=self.get_object()
        # order.status=request.data.get('status')
        # order.save()
        # ser=OrderSerializer(order)
        # return Response(ser.data)
